<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-row type="flex">
        <el-col :span="10">
          <el-form-item label="人员编码" prop="employeeCode">
            <el-input v-model="queryParams.employeeCode" placeholder="请输入人员编码" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="姓名" prop="employeeName">
            <el-input v-model="queryParams.employeeName" placeholder="请输入姓名" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="服务部门" prop="deptId">
            <el-select v-model="queryParams.deptId" placeholder="请选择员服务部门" clearable>
              <el-option v-for="optVal in deptOptions" :key="optVal.deptId" :label="optVal.deptName"
                :value="optVal.deptId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10"></el-col>
      </el-row>

      <el-row type="flex">
        <el-col :span="10">
          <el-form-item label="性别" prop="sex">
            <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
              <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="身份证号" prop="identityId">
            <el-input v-model="queryParams.identityId" placeholder="请输入身份证号" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="学历" prop="education">
            <el-select v-model="queryParams.education" placeholder="请选择学历" clearable>
              <el-option v-for="dict in dict.type.employee_user_education" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="10">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['erecord:employee:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['erecord:employee:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['erecord:employee:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['erecord:employee:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange" @row-click="handleDetail"
       >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="序号" width="55">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="id" align="center" prop="id" v-if="false" />
      <el-table-column label="人员编码" align="center" prop="employeeCode" v-if="true" />
      <el-table-column label="姓名" align="center" prop="employeeName" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="identityId" width="170" />
      <el-table-column label="出生日期" align="center" prop="birthdate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务部门" align="center" prop="deptName" width="120" />
      <el-table-column label="服务类型" align="center" prop="serviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erecord_service_type" :value="scope.row.serviceType" />
        </template>
      </el-table-column>
      <el-table-column label="岗位" align="center" prop="postName" width="110" />
      <el-table-column label="劳动合同" align="center" prop="laborContractUrl">
        <template slot-scope="scope">
          <el-link v-if="scope.row.laborContractUrl" style="font-size: 24px;" icon="el-icon-paperclip"
            :href="scope.row.laborContractUrl" target="_blank" />
          <el-link v-if="!scope.row.laborContractUrl" style="font-size: 24px;" icon="el-icon-document" />
          <!--@click="handleDownload(scope.row.laborContract)"-->
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.employee_user_education" :value="scope.row.education" />
        </template>
      </el-table-column>
      <el-table-column label="职称" align="center" prop="professionalTitle" />
      <el-table-column label="入职日期" align="center" prop="entryDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离职日期" align="center" prop="quitDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.quitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="phone" width="120" />
      <el-table-column label="社保" align="center" prop="socialInsurance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_status_have_or_not" :value="scope.row.socialInsurance" />
        </template>
      </el-table-column>
      <el-table-column label="意外险" align="center" prop="accidentInsurance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_status_have_or_not" :value="scope.row.accidentInsurance" />
        </template>
      </el-table-column>
      <el-table-column label="身份证扫描件" align="center" prop="identityPictureUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.identityPictureUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="资格证书" align="center" prop="qualification">
        <template slot-scope="scope">
          <image-preview v-if="!scope.row.qualificationUrl" :src="scope.row.qualificationUrl" :width="50" :height="50" />
          <span v-if="scope.row.qualificationUrl">

            <image-preview v-for="(quUrl, index) in getUrls(scope.row.qualificationUrl)" v-bind:key="index" :src="quUrl"
              :width="50" :height="50" />
            <!--
            <el-link v-for="(quUrl, index) in getUrls(scope.row.qualificationUrl)" v-bind:key="index"
              style="font-size: 24px;" icon="el-icon-paperclip" :href="quUrl" target="_blank" />
               -->
            <!-- @click="handleDownload(quUrl)" -->

          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['erecord:employee:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['erecord:employee:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="950px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="isDetail">
        <div style="margin: 0px 0px 20px;">
          <label style="font-size: 20px;">基本信息</label>
        </div>

        <el-row type="flex">
          <el-col>
            <!--
        <el-row>
          <el-col :span="12">
            <label style="margin-bottom: 10px;">基本信息</label>
          </el-col>
        </el-row>
        -->

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="人员编码" prop="employeeCode">
                  <el-input v-model="form.employeeCode" placeholder="请输入人员编码" maxlength="20" :readonly="!isAdd" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="姓名" prop="employeeName">
                  <el-input v-model="form.employeeName" placeholder="请输入姓名" maxlength="50" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="性别" prop="sex">
                  <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                      :value="parseInt(dict.value)"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号" prop="identityId">
                  <el-input v-model="form.identityId" placeholder="请输入身份证号" maxlength="18" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="出生日期" prop="birthdate">
                  <el-date-picker clearable v-model="form.birthdate" type="date" value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="请选择出生日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="服务部门" prop="deptId">
                  <el-select v-model="form.deptId" placeholder="请选择员服务部门" clearable>
                    <el-option v-for="optVal in deptOptions" :key="optVal.deptId" :label="optVal.deptName"
                      :value="optVal.deptId" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="手机号码" prop="phone">
                  <el-input v-model="form.phone" placeholder="请输入手机号码" maxlength="13" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="服务类型" prop="serviceType">
                  <el-select v-model="form.serviceType" placeholder="请选择服务类型">
                    <el-option v-for="dict in dict.type.erecord_service_type" :key="dict.value" :label="dict.label"
                      :value="parseInt(dict.value)"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <!--
        <el-row>
          <el-col :span="12">
            <label>扩展信息</label>
          </el-col>
        </el-row>
        -->
            <div style="margin: 20px 0px 20px;">
              <label style="font-size: 20px;">扩展信息</label>
            </div>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="岗位" prop="postId">
                  <el-select v-model="form.postId" placeholder="请选择岗位" clearable>
                    <el-option v-for="optVal in postOptions" :key="optVal.postId" :label="optVal.postName"
                      :value="optVal.postId" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="员工状态" prop="employeeStatus">
                  <el-select v-model="form.employeeStatus" placeholder="请选择员工状态">
                    <el-option v-for="dict in dict.type.employee_user_status" :key="dict.value" :label="dict.label"
                      :value="parseInt(dict.value)"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="职称" prop="professionalTitle">
                  <el-input v-model="form.professionalTitle" placeholder="请输入职称" maxlength="20" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="学历" prop="education">
                  <el-select v-model="form.education" placeholder="请选择学历">
                    <el-option v-for="dict in dict.type.employee_user_education" :key="dict.value" :label="dict.label"
                      :value="parseInt(dict.value)"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="入职日期" prop="entryDate">
                  <el-date-picker clearable v-model="form.entryDate" type="date" value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="请选择入职日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="离职日期" prop="quitDate">
                  <el-date-picker clearable v-model="form.quitDate" type="date" value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="请选择离职日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="身份证扫描件" prop="identityPicture">
                  <image-upload v-model="form.identityPicture" :limit="2" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="社保" prop="socialInsurance">
                  <el-select v-model="form.socialInsurance" placeholder="请选择社保">
                    <el-option v-for="dict in dict.type.common_status_have_or_not" :key="dict.value" :label="dict.label"
                      :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="12">
                <el-form-item label="劳动合同" prop="laborContract">
                  <file-upload v-model="form.laborContract" :limit="10" :fileType="['pdf']" :fileSize="20" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="意外险" prop="accidentInsurance">
                  <el-select v-model="form.accidentInsurance" placeholder="请选择意外险">
                    <el-option v-for="dict in dict.type.common_status_have_or_not" :key="dict.value" :label="dict.label"
                      :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row type="flex">
              <el-col :span="13">
                <el-form-item label="资格证书" prop="qualification">
                  <image-upload v-model="form.qualification" :limit="10" />
                </el-form-item>
              </el-col>

            </el-row>

            <!--
        <el-row>
          <el-col :span="13">
            <el-form-item label="上传头像" prop="avtar">
              <image-upload v-model="form.avtar" :limit="1" />
            </el-form-item>
          </el-col>
        </el-row>
      -->

            <el-row>
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="500" show-word-limit />
              </el-form-item>
            </el-row>

          </el-col>

          <el-col>
            <el-form-item label="上传头像" prop="avtar">
              <image-upload v-model="form.avtar" :limit="1" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer" v-show="!isDetail">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee } from "@/api/erecord/employee";
import { getDeptAndPost } from "@/api/erecord/common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Employee",
  components: { Treeselect },
  dicts: ['employee_user_education', 'employee_user_status', 'sys_user_sex', 'common_status_have_or_not', 'erecord_service_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      showTis: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 岗位选项
      postOptions: [],
      // 是否显示弹出层
      open: false,
      // 是否新增
      isAdd: true,
      isDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeCode: undefined,
        employeeName: undefined,
        sex: undefined,
        identityId: undefined,
        deptId: undefined,
        education: undefined,
        orderByColumn: "update_time,employeeCode",
        isAsc: "desc,asc",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        employeeCode: [
          { required: true, message: "人员编码不能为空", trigger: "blur" }
        ],
        employeeName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        identityId: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "服务部门不能为空", trigger: "blur" }
        ],
        serviceType: [
          { required: true, message: "服务类型不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeptAndPost();
  },
  methods: {
    /** 查询人员列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptAndPost() {
      getDeptAndPost().then(response => {
        this.deptOptions = response.data.depts;
        this.postOptions = response.data.posts;
      });
    },
    getUrls(val) {
      return val.split(',');
    },
    /** 下载按钮操作 */
    handleDownload(ossId) {
      this.$download.oss(ossId)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.isDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        employeeCode: undefined,
        employeeName: undefined,
        sex: undefined,
        identityId: undefined,
        birthdate: undefined,
        deptId: undefined,
        postId: undefined,
        laborContract: undefined,
        education: undefined,
        professionalTitle: undefined,
        entryDate: undefined,
        quitDate: undefined,
        phone: undefined,
        socialInsurance: undefined,
        accidentInsurance: undefined,
        identityPicture: undefined,
        qualification: undefined,
        employeeStatus: undefined,
        avtar: undefined,
        remark: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.showTis = selection.map(item => item.employeeCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isDetail = false;
      this.open = true;
      this.title = "添加人员";
      this.isAdd = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.isAdd = false;
      this.reset();
      const employeeCode = row.id || this.ids
      getEmployee(employeeCode).then(response => {
        this.loading = false;
        this.form = response.data;
        this.isDetail = false;
        this.open = true;
        this.title = "修改人员";
      });
    },
    handleDetail(row) {
      this.loading = true;
      this.isAdd = false;
      this.isDetail = true;
      this.reset();
      const employeeCode = row.id || this.ids
      getEmployee(employeeCode).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "查看人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.isAdd == false) {
            updateEmployee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addEmployee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const employeeCodes = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据').then(() => {
        this.loading = true;
        return delEmployee(employeeCodes);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erecord/employee/export', {
        ...this.queryParams
      }, `人员列表${this.parseTime(new Date().getTime(), '{y}{m}{d}')}.xlsx`)
      //`employee_${new Date().getTime()}.xlsx`
    }
  }
};
</script>
